package com.mc.moring.util;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;



public class TimeUtils {

    //分，秒
    public static String getTimeFromInt(int second) {
        int d = 0;
        int s = 0;
        String s1 = "00";
        d = second / 60;
        if (d == 0) {
            if (second % 60 == 0) {
                s1 = second % 60 + "";
            } else if (second % 60 <= 9) {
                s1 = "0" + second % 60;
            } else {
                s1 = second % 60 + "";
            }
            return "00" + ":" + s1;
        } else if (d < 9) {
            if (second % 60 == 0) {
                s1 = second % 60 + "";
            } else if (second % 60 <= 9) {
                s1 = "0" + second % 60;
            } else {
                s1 = second % 60 + "";
            }
            return "0" + d + ":" + s1;
        } else {
            if (second % 60 == 0) {
                s1 = second % 60 + "";
            } else if (second % 60 <= 9) {
                s1 = "0" + second % 60;
            } else {
                s1 = second % 60 + "";
            }
            return d + ":" + s1 + "";
        }
    }


    public static String timeStamp2Date(String seconds, String format) {
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return "";
        }
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
        return sdf.format(new Date(Long.valueOf(seconds)));
    }


    public static String date2TimeStamp(String date_str, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return String.valueOf(sdf.parse(date_str).getTime() / 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String date2FormatDate(String date_str) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(date_str);
            SimpleDateFormat targetSdf = new SimpleDateFormat("dd-MM-yyyy");
            String formattedDate = targetSdf.format(date);
            return formattedDate;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }

    public static String date2FormatDetailDate(String date_str) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(date_str);
            SimpleDateFormat targetSdf = new SimpleDateFormat("dd-MM-yyyy HH:mm");
            String formattedDate = targetSdf.format(date);
            return formattedDate;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }

    public static String date2LoanHistoryFormatDate(String date_str) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
            Date date = sdf.parse(date_str);
            SimpleDateFormat targetSdf = new SimpleDateFormat("yyy-MM-dd");
            String formattedDate = targetSdf.format(date);
            return formattedDate;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }

    public static String date2UploadFormatDate(String date_str) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
            Date date = sdf.parse(date_str);
            SimpleDateFormat targetSdf = new SimpleDateFormat("dd/MM/yyyy");
            String formattedDate = targetSdf.format(date);
            return formattedDate;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }

    public static String upload2dateFormatDate(String date_str) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            Date date = sdf.parse(date_str);
            SimpleDateFormat targetSdf = new SimpleDateFormat("dd-MM-yyyy");
            String formattedDate = targetSdf.format(date);
            return formattedDate;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }


    public static String date2TradeDetailFormatDate(String date_str) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(date_str);
            SimpleDateFormat targetSdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
            String formattedDate = targetSdf.format(date);
            return formattedDate;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }


    public static String timeStamp() {
        long time = System.currentTimeMillis();
        String t = String.valueOf(time / 1000);
        return t;
    }


    public static String timeStamp2() throws ParseException {
        String DEFAULT_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
        SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT);
        String dateStr = dateFormatter.format(Calendar.getInstance().getTime());//获取当前时间
        SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH);//输入的被转化的时间格式
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = null;
        date1 = dff.parse(dateStr);
        String str2 = df2.format(date1);
        return str2;
    }


    public static String timeDiffer(Date begin, Date end) {
        long lDate1 = begin.getTime();
        long lDate2 = end.getTime();
        long diff = (lDate1 < lDate2) ? (lDate2 - lDate1) : (lDate1 - lDate2);
        long day = diff / (24 * 60 * 60 * 1000);
        long hour = diff / (60 * 60 * 1000) - day * 24;
        long min = diff / (60 * 1000) - day * 24 * 60 - hour * 60;
        long sec = diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60;
        return (day + 1) + "";
    }


    public static Date stringToDate(String strTime, String formatType) {
        Date date = null;
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(formatType);
            date = formatter.parse(strTime);
            return date;
        } catch (ParseException e) {
        }
        return date;
    }


    @SuppressLint("SimpleDateFormat")
    public static String dateToStamp(String s) {
        String res = null;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = simpleDateFormat.parse(s);
            long ts = date.getTime();
            res = String.valueOf(ts);
            return res;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return res;
    }


    public static String getMonthFirstLastDate(Date date) throws ParseException {
        SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
        //int maximum = getMaxDayByYearMonth(date);
        calendar.set(Calendar.DATE, 1);
        Date lastDate = calendar.getTime();
        return dataFormat.format(lastDate);
    }


    public static String getMonthFirstDate(Date date) throws ParseException {
        SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
        //int maximum = getMaxDayByYearMonth(date);
        calendar.set(Calendar.DATE, 1);
        Date lastDate = calendar.getTime();
        return dataFormat.format(lastDate);
    }


    public static int getMaxDayByLastDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.YEAR, date.getYear());
        calendar.set(Calendar.MONTH, date.getMonth() - 1);
        return calendar.getActualMaximum(Calendar.DATE);
    }


    public static int getMaxDayByDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.YEAR, date.getYear());
        calendar.set(Calendar.MONTH, date.getMonth());
        return calendar.getActualMaximum(Calendar.DATE);
    }


    public static long calculateOverdueDays(String repaymentDateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date repaymentDate = sdf.parse(repaymentDateStr);
            Date currentDate = new Date();

            long diffInMillies = Math.abs(currentDate.getTime() - repaymentDate.getTime());
            return diffInMillies / (24 * 60 * 60 * 1000);

        } catch (ParseException e) {
            e.printStackTrace();
            return -1;
        }
    }


    public static String getDateAfter24Hours() {

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR_OF_DAY, 24);

        Date date = calendar.getTime();

        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
        return sdf.format(date);
    }

}
